import Vue from "vue";
import { sleep } from "Yan/utils";
/**
* @comoonent 组件
* @option 组件传入参数 props
*/
export default function defaultObj(component, option = {}) {
  // console.log(component)
  const EditerCreater = Vue.extend(component)
  const Editer = new EditerCreater()
  // eslint-disable-next-line space-before-function-paren
  const runClose = async () => {
    // console.log('runClose')
    Editer.close()
    await sleep(200)
    Editer.$destroy()
    // document.body.removeChild(Editer.$el)
  }
  if (Editer.$props) {
    const opKey = Object.keys(Editer.$props)
    opKey.forEach(i => {
      if (typeof option[i] !== 'undefined') {
        if (typeof option[i] === 'function') {
          Editer.$props[i] = (e) => option[i](e, runClose)
        } else Editer.$props[i] = option[i]
      }
    })
    Editer.$props.runClose = runClose
  }
  Editer.$mount()
  // console.log('dddd', Editer)
  if (option.ElementKey) document.querySelector(option.ElementKey).appendChild(Editer.$el)
  else document.body.appendChild(Editer.$el)
}
